import zipfile
import optparse
from threading import Thread
import time


# start_time = time.clock()


def extractFile(zFile, password):
    try:
        zFile.extratall(pwd=password)
        print('[+] find password')
    except:
        pass


def main():
    parse = optparse.OptionParser("Usage: %prog [options] arg1 arg2")
    parse.add_option('-f', dest='file', type='string', help='Load the file')
    parse.add_option('-d', dest='dict', type='string', help='Loading the dictionary')
    # TODO 添加自定义线程数量,提高破解速度
    parse.add_option('-t', dest="thread", type="str", help="Set the thread")
    (options, args) = parse.parse_args()

    if (options.file) == None and (options.dict) == None:
        print(parse.usage)
        exit(0)
    else:
        file = options.file
        dict = options.dict

    zFile = zipfile.ZipFile(file)
    dic = open(dict,'r')
    for i in dic.readline():
        password = i.strip('\n')
        print('[+]',password,end='')
        t = Thread(target=extractFile(), args=(zFile, password,))
        t.start()


if __name__ == '__main__':
    main()
    # end_time = time.clock()
    # print('use time is '+end_time-start_time+'s')
